﻿<!--#include file="config.asp"-->
<%	
''' SDCMS 财付通通知
''' ==================================================================
''' 版权所有 http://www.sdcms.cn
''' ------------------------------------------------------------------
''' 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
''' 未经授权不允许对程序代码以任何形式任何目的的再发布。
''' ==================================================================
''' 编写: IT平民
''' 修改：IT平民 in 2014.07

	sub callback()
		dim cmdno,pay_result,date_,transaction_id,sp_billno,total_fee,fee_type,attach,sign
		dim md5str,md5sign
		cmdno=sdcms.enhtml(sdcms.fquery("cmdno",0))
		pay_result=sdcms.enhtml(sdcms.fquery("pay_result",0))
		date_=sdcms.enhtml(sdcms.fquery("date",0))
		transaction_id=sdcms.enhtml(sdcms.fquery("transaction_id",0))
		sp_billno=sdcms.enhtml(sdcms.fquery("sp_billno",0))
		total_fee=sdcms.enhtml(sdcms.fquery("total_fee",0))
		fee_type=sdcms.enhtml(sdcms.fquery("fee_type",0))
		attach=sdcms.enhtml(sdcms.fquery("attach",0))
		sign=sdcms.enhtml(sdcms.fquery("sign",0))
		
		if sdcms.strlen(cmdno)=0 or sdcms.strlen(pay_result)=0 or sdcms.strlen(date_)=0 or sdcms.strlen(transaction_id)=0 or sdcms.strlen(sp_billno)=0 or sdcms.strlen(total_fee)=0 or sdcms.strlen(fee_type)=0 or sdcms.strlen(sign)=0 then
			sdcms.echo "充值失败(参数不完整)，<a href="""&webroot&""">返回网站</a>"
			exit sub
		end if
		
		md5str=""
		md5str="cmdno="&cmdno
		md5str=md5str&"&pay_result="&pay_result
		md5str=md5str&"&date="&date_
		md5str=md5str&"&transaction_id="&transaction_id
		md5str=md5str&"&sp_billno="&sp_billno
		md5str=md5str&"&total_fee="&total_fee
		md5str=md5str&"&fee_type="&fee_type
		md5str=md5str&"&attach="&attach
		md5str=md5str&"&key="&appkey
		md5sign=MD5_32(md5str)

		if md5sign<>lcase(sign) then
			sdcms.echo "充值失败(签名错误)，<a href="""&webroot&""">返回网站</a>"
			exit sub
		else
			if pay_result="0" then
				dim data:data=sdcms.db.dbload(1,"id,isover,amount,userid","sd_user_payrecord","orderid='"&sp_billno&"' and payway='财付通'","")
				if ubound(data)>=0 then
					if data(1,0)=0 then
						sdcms.db.dbupdate "sd_user_payrecord","id="&data(0,0)&"",array(array("isover",1,0,0))
						dim userid:userid=data(3,0)
						dim userdata:userdata=sdcms.db.dbload(1,"amount","sd_user","id="&userid&"","")
						if ubound(userdata)=0 then
							dim amount:amount=sdcms.getint(userdata(0,0),0)+data(2,0)
							sdcms.db.dbupdate "sd_user","id="&userid&"",array(array("amount",amount,10,0))
							sdcms.createusermoney array(array("amount",data(2,0),0,0),array("userid",userid,0,0),array("type",1,0,0),array("content","财付通充值，交易号："&transaction_id&"",0,1),array("createdate",sqltime,0,0))
						end if
					end if
				end if
				dim strHtml
				strHtml="<html><head>"&vbcrlf
				strHtml=strHtml&"<meta name=""TENCENT_ONLINE_PAYMENT"" content=""China TENCENT"">"&vbcrlf
				strHtml=strHtml&"<script language=""javascript"">"&vbcrlf
				strHtml=strHtml&	"window.location.href='show.asp?t0="&total_fee/100&"';"&vbcrlf
				strHtml=strHtml&"</script>"&vbcrlf
				strHtml=strHtml&"</head><body></body></html>"
				sdcms.echo strHtml
			else
				sdcms.echo "充值失败(彻底失败)，<a href="""&webroot&""">返回网站</a>"
			end if
		end if
	end sub

	callback()
	sdcms.db.dbclose
%>